import requests
from bs4 import BeautifulSoup
import pymysql

# ����Ҫ��������ҳ URL
url = 'https://wandou.la/hot/movie'

# ���� GET �����ȡ��ҳ����
response = requests.get(url)

# ��������Ƿ�ɹ�
if response.status_code == 200:
    # ������ҳ����
    soup = BeautifulSoup(response.text, 'html.parser')

    # ��ȡ��ҳ�е��ض�Ԫ�ػ���Ϣ
    title = soup.title.string  # ��ȡ��ҳ����
    paragraphs = soup.find_all('p', attrs={"class": "name"})  # �ҵ����� <p> Ԫ�� class="name"

    # for paragraph in paragraphs:
    #     print(paragraph.get_text())  # ��ӡÿ��������ı�����

    links = soup.find_all('img', attrs={"class": "preview-img"})  # ��ȡ��Ӱ��ͼ

    #评分
    scores = soup.find_all("div", attrs={"class": "c-hDImTG"})

    # 主角
    actors = soup.find_all("p", attrs={"class": "names"})

    conn = pymysql.connect(host='192.168.18.13', port=33036, user='root', password='123456', db='chencanxian',
                           charset='utf8')


    # �����α�
    cursor = conn.cursor()

    scores_cln = [] # 保
    #遍
    for score in scores:
        result = score.get_text().replace("豆", "").strip()
        scores_cln.append(result)


    for index, paragraph in enumerate(paragraphs):
        # ִ�� SQL ���
        sql = "INSERT INTO movies (name, pic, score, actor) VALUES (%s, %s, %s, %s)"
        link = links[index]
        score = scores_cln[index]
        actor = actors[index]
        val = (paragraph.get_text(), link["src"], score, actor.get_text())
        cursor.execute(sql, val)


     # for index, link in enumerate(links):
     #     print(link["src"])  # ��ȡimg��ǩ������
     #
     #    # ִ�� SQL ���
     #
     #     val = (link["src"], paragraphs[index].get_text())
     #     print(val)
     #     sql = "update movies set pic='%s' where name='%s'" % val
     #     print(sql)
     #     cursor.execute(sql)

    # �ύ����
    conn.commit()

    # �ر��α������
    cursor.close()
    conn.close()


else:
    print("����ʧ�ܣ�״̬�룺", response.status_code)